<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Blocks</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>{...} (block)</h1>

<p>A pair of braces denotes a block. Blocks are typically used with <a href="../Functions.htm">functions</a>, <a href="Else.htm">Else</a>, <a href="Loop.htm">Loop</a>, <a href="While.htm">While-loop</a>, and IF-commands.</p>

<pre class="Syntax">
{
zero or more commands
}</pre>
<h3>Remarks</h3>
<p>A block is used to bind two or more commands together. It can also be used to change which IF an <a href="Else.htm">ELSE</a>  belongs to, as in this example where the block forces the <a href="Else.htm">ELSE</a> to belong to the first IF rather than the second:</p>
<pre>if var1 = 1
{
    if var2 = abc
        sleep, 1
}
else
    return</pre>
<p>Although blocks can be used anywhere, currently they are only meaningful when used with <a href="../Functions.htm">functions</a>, <a href="Else.htm">Else</a>, <a href="Loop.htm">Loop</a>, or an IF-type command such as <a href="IfEqual.htm">IfEqual</a> or <a href="WinExist.htm">IfWinExist</a>.</p>
<p>If an <a href="IfEqual.htm">IF</a>, <a href="Else.htm">ELSE</a>, <a href="Loop.htm">Loop</a>, <a href="While.htm">While-loop</a>, or <a href="For.htm">For-loop</a> has only a single command, that command need not be enclosed in a block. However, there may be cases where doing so enhances the readability or maintainability of the script.</p>
<p>A block may be empty (contain zero commands), which may be useful in cases where you want to comment out the contents of the block without removing the block itself.</p>
<p><a name="otb"></a><strong>One True Brace (OTB, K&amp;R style):</strong> The OTB style may optionally be used in the following places: <a href="IfExpression.htm">expression if-statements</a>, the <a href="Else.htm">else</a> keyword, <a href="While.htm">while-loops</a>, <a href="For.htm">For-loops</a>, <a href="Loop.htm">normal loops</a>, <a href="../Functions.htm#define">function definitions</a>, <a href="Try.htm">Try</a>, <a href="Catch.htm">Catch</a>, and <a href="Finally.htm">Finally</a>. This style puts the block's opening brace on the same line as the block's controlling statement rather than underneath on a line by itself. For example:</p>
<pre>if (x &lt; y) <strong>{</strong>
    ...
<strong>}</strong> else <strong>{</strong>
    ...
<strong>}</strong>
While x &lt; y <strong>{</strong>
    ...
<strong>}</strong>
For k,v in obj <strong>{</strong>
    ...
<strong>}</strong>
Loop %RepeatCount% <strong>{</strong>
    ...
<strong>}</strong>
MyFunction(x, y) <strong>{</strong>
    ...
<strong>}</strong>
Try <strong>{</strong>
    ...
<strong>}</strong> Catch e <strong>{</strong>
    ...
<strong>}</strong> Finally <strong>{</strong>
    ....
<strong>}</strong></pre>
<p>Similarly, a command or other action may exist to the right of a brace (except the open-brace of the One True Brace style). For example:</p>
<pre>if x = 1
{ MsgBox This line appears to the right of an opening brace. It executes whenever the IF-statement is true.
    MsgBox This is the next line.
} MsgBox This line appears to the right of a closing brace. It executes unconditionally.</pre>
<h3>Related</h3>
<p><a href="../Functions.htm">Functions</a>, <a href="While.htm">While-loop</a>, <a href="Loop.htm">Loop</a>, <a href="Else.htm">Else</a>, <a href="IfEqual.htm">If</a>, <a href="IfExpression.htm">If(Expression)</a></p>
<h3>Example</h3>
<pre class="NoIndent">if x = 1
{
    MsgBox, test1
    Sleep, 5
}
else
    MsgBox, test2</pre>

</body>
</html>
